<?php

namespace App\Http\Resources;

use App\Models\Polling;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class PollingPlanResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'number' => $this->number,
            'status' => $this->status,
            'status_text' => $this->status_text,
            'plan_start_at' => $this->plan_start_at,
            'plan_end_at' => $this->plan_end_at,
            'circle_unit' => $this->circle_unit,
            'circle_unit_text' => $this->circle_unit_text,
            'circle_count' => $this->circle_count,
            'plan_at' => $this->plan_at,
            'create_user_id' => $this->create_user_id,
            'create_user_name' => $this->create_user_name,
            'polling_user_id' => $this->polling_user_id,
            'polling_user_name' => $this->polling_user_name,
            'polling_customer_id' => $this->polling_customer_id,
            'polling_customer_name' => $this->polling_customer_name,
            'polling_customer_user_id' => $this->polling_customer_user_id,
            'polling_customer_user_name' => $this->polling_customer_user_name,
            'polling_customer_user_telephone' => $this->polling_customer_user_telephone,
            'polling_customer_address' => $this->polling_customer_address,
            'polling_description' => $this->polling_description,
            'btn_delete' => $this->btn_delete,
            'btn_pause' => $this->btn_pause,
            'btn_start' => $this->btn_start,
            'create_time' => Carbon::parse($this->create_time)->toDateTimeString(),
            'update_time' => Carbon::parse($this->update_time)->toDateTimeString(),
            'logs' => $this->getLogs()
        ];
    }

    private function getLogs()
    {
        $logs = $this->logs;
        if (empty($logs)) {
            return [];
        }
        $results = [];
        foreach ($logs as $log) {
            $polling = Polling::find($log->polling_id);
            $results[] = [
                'id' => $log->id,
                'polling_plan_id' => $log->polling_plan_id,
                'polling_id' => $log->polling_id,
                'polling_number' => $polling->number ?? '',
                'customer_id' => $this->polling_customer_id,
                'customer_name' => $this->polling_customer_name,
                'customer_user_id' => $this->polling_customer_user_id,
                'customer_user_name' => $this->polling_customer_user_name,
                'customer_user_telephone' => $this->polling_customer_user_telephone,
                'customer_address' => $this->polling_customer_address,
                'plan_at' => $this->plan_at,
                'status' => $log->status,
                'status_text' => '成功',
                'create_time' => Carbon::parse($log->create_time)->toDateTimeString()
            ];
        }
        return $results;
    }
}
